home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 2 / CU Amiga Magazine's Super CD-ROM 02 (1996)(EMAP Images)(GB)[!][issue 1996-04].iso / magazine / amiga_e / jrh-rkrm-partone / commodities / hotkey.e < prev    next >
Text File  |  1995-04-01  |  7KB  |  164 lines

  1. -> HotKey.e - Simple hot key commodity
  2.  
  3. OPT PREPROCESS  -> E-Note: we are using the CxXXX creation macros
  4.  
  5. MODULE 'commodities',
  6.        'icon',
  7.        'amigalib/argarray',
  8.        'dos/dos',
  9.        'exec/ports',
  10.        'libraries/commodities'
  11.  
  12. ENUM ERR_NONE, ERR_ARG, ERR_BRKR, ERR_CRCX, ERR_CXERR, ERR_LIB, ERR_PORT
  13.  
  14. RAISE ERR_BRKR IF CxBroker()=NIL,
  15.       ERR_CRCX IF CreateCxObj()=NIL,  -> E-Note: the CxXXX macros use this
  16.       ERR_LIB  IF OpenLibrary()=NIL,
  17.       ERR_PORT IF CreateMsgPort()=NIL
  18.  
  19. CONST EVT_HOTKEY=1
  20.  
  21. DEF broker_mp=NIL:PTR TO mp, broker=NIL, filter=NIL, sender, translate,
  22.     cxsigflag
  23.  
  24. PROC main() HANDLE
  25.   DEF hotkey, ttypes=NIL, msg
  26.   cxbase:=OpenLibrary('commodities.library', 37)
  27.   -> Open the icon.library for the support functions, argArrayXXX()
  28.   iconbase:=OpenLibrary('icon.library', 36)
  29.   broker_mp:=CreateMsgPort()
  30.   cxsigflag:=Shl(1, broker_mp.sigbit)
  31.  
  32.   -> argArrayInit() is a support function (from the 2.0 version of amiga.lib)
  33.   -> that makes it easy to read arguments from either a CLI or from Workbench's
  34.   -> ToolTypes.  Because it uses icon.library, the library has to be open before
  35.   -> calling this function.  argArrayDone() cleans up after this function.
  36.   -> E-Note: argArrayInit() needs no arguments: it uses global arg and wbmessage
  37.   IF (ttypes:=argArrayInit())=NIL THEN Raise(ERR_ARG)
  38.  
  39.   broker:=CxBroker([NB_VERSION, 0,
  40.                    'RKM HotKey',   -> String to identify this broker
  41.                    'A Simple Hotkey',
  42.                    'A simple hot key commodity',
  43.                     -> Don't want any new commodities starting with this name.
  44.                     -> If someone tries it, let me know
  45.                     NBU_UNIQUE OR NBU_NOTIFY,
  46.   -> argInt() (also in amiga.lib) searches through the array set up by
  47.   -> argArrayInit() for a specific ToolType.  If it finds one, it returns the
  48.   -> numeric value of the number that followed the ToolType (e.g.,
  49.   -> CX_PRIORITY=7).  If it doesn't find the ToolType, it returns the default
  50.   -> value (the third argument).
  51.                     0, argInt(ttypes, 'CX_PRIORITY', 0), 0,
  52.                     broker_mp, 0]:newbroker, NIL)
  53.  
  54.   -> argString() works just like argInt(), except it returns a pointer to a
  55.   -> string rather than an integer.  In the example below, if there is no
  56.   -> ToolType 'HOTKEY', the function returns a pointer to 'rawkey control esc'.
  57.   hotkey:=argString(ttypes, 'HOTKEY', 'rawkey control esc')
  58.  
  59.   -> CxFilter() is a macro that creates a filter CxObject.  This filter passes
  60.   -> input events that match the string pointed to by hotkey.
  61.   filter:=CxFilter(hotkey)
  62.   -> Add a CxObject to another's personal list
  63.   AttachCxObj(broker, filter)
  64.  
  65.   -> CxSender() creates a sender CxObject.  Every time a sender gets a
  66.   -> CxMessage, it sends a new CxMessage to the port pointed to in the first
  67.   -> argument.  CxSender()'s second argument will be the ID of any CxMessages
  68.   -> the sender sends to the port.  The data pointer associated with the
  69.   -> CxMessage will point to a *COPY* of the InputEvent structure associated
  70.   -> with the orginal CxMessage.
  71.   sender:=CxSender(broker_mp, EVT_HOTKEY)
  72.   AttachCxObj(filter, sender)
  73.  
  74.   -> CxTranslate() creates a translate CxObject.  When a translate CxObject
  75.   -> gets a CxMessage, it deletes the original CxMessage and adds a new input
  76.   -> event to the input.device's input stream after the Commodities input
  77.   -> handler.  CxTranslate's argument points to an InputEvent structure from
  78.   -> which to create the new input event.  In this example, the pointer is NIL,
  79.   -> meaning no new event should be introduced, which causes any event that
  80.   -> reaches this object to disappear from the input stream.
  81.   translate:=CxTranslate(NIL)
  82.   AttachCxObj(filter, translate)
  83.  
  84.   -> CxObjError() is a commodities.library function that returns the internal
  85.   -> accumulated error code of a CxObject.
  86.   IF CxObjError(filter)<>FALSE THEN Raise(ERR_CXERR)
  87.  
  88.   ActivateCxObj(broker, TRUE)
  89.   processMsg()
  90.  
  91. EXCEPT DO
  92.   -> DeleteCxObjAll() is a commodities.library function that not only deletes
  93.   -> the CxObject pointed to in its argument, but it deletes all of the
  94.   -> CxObjects that are attached to it.
  95.   IF broker THEN DeleteCxObjAll(broker)
  96.   IF broker_mp
  97.     WHILE msg:=GetMsg(broker_mp) DO ReplyMsg(msg)
  98.     DeleteMsgPort(broker_mp) -> E-Note: C version incorrectly uses DeletePort()
  99.   ENDIF
  100.   IF ttypes THEN argArrayDone()
  101.   IF iconbase THEN CloseLibrary(iconbase)
  102.   IF cxbase THEN CloseLibrary(cxbase)
  103.   SELECT exception
  104.   CASE ERR_ARG;   WriteF('Error: Could not init arg array\n')
  105.   CASE ERR_BRKR;  WriteF('Error: Could not create broker\n')
  106.   CASE ERR_CRCX;  WriteF('Error: Could not create CX object\n')
  107.   CASE ERR_CXERR; WriteF('Error: Could not activate broker\n')
  108.   CASE ERR_LIB;   WriteF('Error: Could not open required library\n')
  109.   CASE ERR_PORT;  WriteF('Error: Could not create message port\n')
  110.   ENDSELECT
  111. ENDPROC
  112.  
  113. PROC processMsg()
  114.   DEF msg, sigrcvd, msgid, msgtype, done=FALSE
  115.   REPEAT
  116.     sigrcvd:=Wait(SIGBREAKF_CTRL_C OR cxsigflag)
  117.     WHILE msg:=GetMsg(broker_mp)
  118.       msgid:=CxMsgID(msg)
  119.       msgtype:=CxMsgType(msg)
  120.       ReplyMsg(msg)
  121.       SELECT msgtype
  122.       CASE CXM_IEVENT
  123.         WriteF('A CXM_IEVENT, ')
  124.         SELECT msgid
  125.         CASE EVT_HOTKEY  -> We got the message from the sender CxObject
  126.           WriteF('You hit the HotKey.\n')
  127.         DEFAULT
  128.           WriteF('unknown.\n')
  129.         ENDSELECT
  130.       CASE CXM_COMMAND
  131.         WriteF('A command: ')
  132.         SELECT msgid
  133.         CASE CXCMD_DISABLE
  134.           WriteF('CXCMD_DISABLE\n')
  135.           ActivateCxObj(broker, FALSE)
  136.         CASE CXCMD_ENABLE
  137.           WriteF('CXCMD_ENABLE\n')
  138.           ActivateCxObj(broker, TRUE)
  139.         CASE CXCMD_KILL
  140.           WriteF('CXCMD_KILL\n')
  141.           done:=TRUE
  142.         CASE CXCMD_UNIQUE
  143.           -> Commodities Exchange can be told not only to refuse to launch a
  144.           -> commodity with a name already in use but also can notify the
  145.           -> already running commodity that it happened.  It does this by
  146.           -> sending a CXM_COMMAND with the ID set to CXMCMD_UNIQUE.  If the
  147.           -> user tries to run a windowless commodity that is already running,
  148.           -> the user wants the commodity to shut down.
  149.           WriteF('CXCMD_UNIQUE\n')
  150.           done:=TRUE
  151.         DEFAULT
  152.           WriteF('Unknown msgid\n')
  153.         ENDSELECT
  154.       DEFAULT
  155.         WriteF('Unknown msgtype\n')
  156.       ENDSELECT
  157.     ENDWHILE
  158.     IF sigrcvd AND SIGBREAKF_CTRL_C
  159.       WriteF('CTRL C signal break\n')
  160.       done:=TRUE
  161.     ENDIF
  162.   UNTIL done
  163. ENDPROC
  164.